import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:gczn_hd/app/core/base/base_view.dart';
import 'package:gczn_hd/app/core/values/app_colors.dart';
import 'package:gczn_hd/app/core/values/app_values.dart';
import 'package:gczn_hd/app/core/values/text_styles.dart';
import 'package:gczn_hd/app/core/widget/custom_app_bar.dart';
import 'package:gczn_hd/app/core/widget/gczn_page.dart';
import 'package:gczn_hd/app/routes/app_pages.dart';

import 'package:get/get.dart';

import '../controllers/qc_check_list_controller.dart';

class QcCheckListView extends BaseView<QcCheckListController> {
  
  @override
  PreferredSizeWidget? appBar(BuildContext context) => CustomAppBar(
    appBarTitleText: "检验记录",
    actions: [
      ElevatedButton(
          onPressed: (){
            // TODO
            Get.toNamed(Routes.CHECK_DETAIL);
          },
          style: btnStyle,
          child: Text("新增检验", style: submitButtonText,)
      ),
      SizedBox(width: AppValues.size20.w,)
    ]
    );

  @override
  Widget body(BuildContext context) {

    return GcznPage(
      easyController: controller.gcznPageController.value,
      onRefresh: () async {
        await controller.onRefresh();
      },
      onLoadNextPage: () async {
        await controller.onLoadNextPage();
      },
      child: SingleChildScrollView(
        child: Container(
          margin: EdgeInsets.only(top: AppValues.size40.h),
          padding: EdgeInsets.only(left: AppValues.size40.w, right: AppValues.size40.w),
          child: Wrap(
            spacing: AppValues.size36, // 水平间距
            runSpacing: AppValues.size36, // 垂直间距
            children: [1,2,3,4,5,6,7,8].map((item)=>_buildItem(context, idx: item)).toList(),
          ),
        ),
      ),
    );
    
  }


  Widget _buildItem(BuildContext context, {int idx = 1}) {

    return GestureDetector(
      onTap: (){
        // TODO
        Get.toNamed(Routes.CHECK_DETAIL);
      },
      child: Container(
         width: (MediaQuery.of(context).size.width - 3 * AppValues.size36 - AppValues.size20) / 3, // 计算每个子项的宽度
          decoration: BoxDecoration(
            border: Border.all(
              color: const Color.fromRGBO(39, 65, 80, 1),
              width: 1.0,
            ),
            borderRadius: BorderRadius.circular(AppValues.margin_12.r),
            color: AppColors.processBtn,
            boxShadow: const [pressShadow],
          ),
          clipBehavior: Clip.hardEdge,
          child: Column(
            children: [
              _buildTitleBar(idx),
              _buildOrderMsg(),
              _buildFooter()
            ],
          )
      ),
    );
  }


    Widget _buildFooter(){

    return Container(
      margin: EdgeInsets.only(left: AppValues.size37.w, right: AppValues.size37.w,),
      padding: const EdgeInsets.only( top: 10, bottom: 10),
      decoration: const BoxDecoration(
        border: Border(top: BorderSide(color: Color.fromRGBO(39, 65, 80, 1), width: 1))
      ),
      child: Row(
        children: [
          Expanded(child:Text("主检：冠生", style: textStyle20Opa8,) ),
          Text("2024-8-19 18:09:11", style: textStyle20Opa8,)
        ],
      ),
    );
  }

  Widget _buildOrderMsg(){
    List<Map<String, String>> list = [
      {"name": "任务单号", "value": "WO2024000067"},
      {"name": "检验产品", "value": "扇叶注塑/工位A01"},
      {"name": "检验类型", "value": "格力空调循环落地风扇"},
      {"name": "总数/不良数", "value": "100/3"},
    ];
    const double labelWidth = 130.0;

    return Container(
      padding: EdgeInsets.only(left: AppValues.size37.w, right: AppValues.size37.w, top: AppValues.size30.h, bottom: AppValues.size30.h),
      child: Column(
        children: list.map((item) => Container(
          margin: const EdgeInsets.only(bottom: 7),
          child: Row(
            children: [
              SizedBox(
                width: labelWidth.w,
                child: Text("${item['name']}: ", style: textStyle20Opa8,),
              ),
              Expanded(child: Text(item['value']!, style: textStyle, maxLines: 1, overflow: TextOverflow.ellipsis,))
            ],
          ),
        )).toList(),
      ),
    );
  }


  Widget _buildTitleBar(int idx){
    const double barHeight = 73.0;

    return Container(
      height: barHeight.h,
      padding: EdgeInsets.only(left: AppValues.size37.w, right: AppValues.size37.w),
      color: AppColors.titlebarBackground,
      child: Row(
        children: [
          Expanded(
            flex: 1,
            child: Text("RP2024000009", style: submitButtonText,),
          ),
          idx == 1 ? Text("待检验", style: toggleStyle('待检验'),) : idx == 2 ? Text("待审核", style: toggleStyle('待审核'),) : Text("已完成", style: toggleStyle('已完成'),) ,
        ],
      ),
    );
  }

  TextStyle toggleStyle(String text){
    if( text == '待检验' ) {
      return waitTextStyle;
    } else if( text == '待审核' ) {
      return fialTextStyle;
    } else {
      return  successTextStyle;
    }
  }



}
